package com.atguigu03.threadsafe.exer;

/**
 * ClassName: AcccontTest
 * Package: com.atguigu03.threadsafe.exer
 * Description:
 *
 * @Author ZY
 * @Create 17:09
 * @Version 1.0
 */
public class AccountTest {
    public static void main(String[] args) {

        Account acct = new Account();

        Customer c1 = new Customer(acct,"甲");
        Customer c2 = new Customer(acct,"乙");

        c1.start();
        c2.start();
    }

}

class Account{//账户
    private double balance;//余额

    public synchronized void deposit(double amt){//此时this为acct，且唯一的
        if(amt > 0){
            balance += amt;
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName() + "存钱1000块，余额为： " + balance);
    }
}

class Customer extends Thread{
    Account account;

    public Customer(Account account) {
        this.account = account;
    }

    public Customer(Account account,String name) {
        super(name);
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}
